package com.example.springbolgdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbolgdemo.contstant.Contstants;
import com.example.springbolgdemo.mapper.BlogInfoMapper;
import com.example.springbolgdemo.pojo.dataobject.BlogInfo;
import com.example.springbolgdemo.pojo.request.AddBlogRequest;
import com.example.springbolgdemo.pojo.request.UpdateBlogRequest;
import com.example.springbolgdemo.pojo.response.BlogDetailResponse;
import com.example.springbolgdemo.pojo.response.BlogInfoResponse;
import com.example.springbolgdemo.service.BlogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("blogService")
public class BlogServiceImpl implements BlogService {

    //使用@Mapper时会自动注册类名首字母小写的bean
    @Resource(name="blogInfoMapper")
    private BlogInfoMapper blogMapper;

    @Override
    public List<BlogInfoResponse> getList() {

        //通过wrapper构造查询条件,Lambda形式可以通过::表达式获取到实体类的元素字段
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Contstants.NOT_DELETE);
        List<BlogInfo> blogInfos=blogMapper.selectList(queryWrapper);


        List<BlogInfoResponse> blogInfoResponseList = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            //将BlogInfo中与BlogInfoResponse相同的元素复制过去
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());

        return blogInfoResponseList;
    }

    @Override
    public BlogDetailResponse getBlogDetail(Integer blogId) {

        BlogInfo blogInfo = blogMapper.selectById(blogId);
        //进行类型转换
        BlogDetailResponse blogDetailResponse=new BlogDetailResponse();
        BeanUtils.copyProperties(blogInfo,blogDetailResponse);

        return blogDetailResponse;
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(addBlogRequest,blogInfo);
        try {
            Integer result=insertBlog(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("发布博客失败,e:",e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo=new BlogInfo();
        BeanUtils.copyProperties(updateBlogRequest,blogInfo);
        try {
            Integer result=updateBlog(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("更新博客失败,e:",e);
        }
        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Contstants.IS_DELETE);
        try {
            Integer result=updateBlog(blogInfo);
            return result==1;
        }catch (Exception e){
            log.error("删除博客失败,e:",e);
        }
        return false;
    }

    public Integer insertBlog(BlogInfo blogInfo){
        return blogMapper.insert(blogInfo);
    }

    public Integer updateBlog(BlogInfo blogInfo){
        return blogMapper.updateById(blogInfo);
    }
}
